﻿Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine

Public Class frmVacationBalances

    Private Sub frmVacationBalances_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub NumericUpDown1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles txtYear.ValueChanged
        Me.CrystalReportViewer1.ReportSource = Nothing
    End Sub

    Private Sub btnGenerate_Click(sender As System.Object, e As System.EventArgs) Handles btnGenerate.Click
        Try
            Me.Cursor = Cursors.WaitCursor

            'Dim dap As New SqlDataAdapter("Select EmpID,EmpName,Sum(NoOfDays) NoOfDays " & _
            '                              "From StaffLeave Where Year(TransDate)=" & Me.txtYear.Value & _
            '                              "Group By EmpID,EmpName", cnn)

            Dim dap1 As New SqlDataAdapter("Select *,dbo.GetVacationDaysTaken(EmpNo," & Me.txtYear.Value & ") VacationDaysTaken From StaffProfiles", cnn)
            Dim das As New DsVacations

            'dap.Fill(das, "StaffLeave")
            dap1.Fill(das, "StaffProfiles")

            Dim rpt As New VacationBalances
            rpt.SetDataSource(das)

            Dim crParameterDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue
            Dim crParameterFieldDefinitions As ParameterFieldDefinitions
            Dim crParameterFieldLocation As ParameterFieldDefinition
            Dim crParameterValues As New CrystalDecisions.Shared.ParameterValues
            crParameterFieldDefinitions = rpt.DataDefinition.ParameterFields

            'Year PARAMETER
            crParameterFieldLocation = crParameterFieldDefinitions.Item("Year")
            crParameterValues = crParameterFieldLocation.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            crParameterDiscreteValue.Value = Me.txtYear.Value
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

            Me.CrystalReportViewer1.ReportSource = rpt
          
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class